package edu.hcnu.service;

import edu.hcnu.entity.Car;
import edu.hcnu.mapper.CarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/4/29 19:19
 */
@Service
public class CarService {

    @Autowired
    private CarMapper carMapper;
    // 清除缓存
    @CacheEvict(cacheNames = "getAllCar",condition ="#result == true")
    public String add(Car car) {
        // 确保数据库中没有这个车次的车
        List<String > ids = new ArrayList<String>();
        ids.add(car.getId());
        List<Car> list = carMapper.queryBatchIds(ids);
        if(list!=null && list.size()>=1){
            return "已经存在这个车次 不能重复添加车次 请确保你的车次信息输入正确";
        }
        carMapper.add(car);
        return "success";
    }

   // 清除缓存
   @CacheEvict(cacheNames = "getAllCar")
    public boolean deleteCar(String cid) {
        return carMapper.deleteByCID(cid);
    }

    @Cacheable(cacheNames = "getAllCar")
    public List<Car> getAllCar() {
        return  carMapper.getAllCar();
    }
}
